package com.joe.dom4j;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class testDom4j {
	
	public Document parse(String fileUrl){
		File file = new File(fileUrl);
		SAXReader saxReader = new SAXReader();
		Document document = null;
		try {
			document = saxReader.read(file);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return document;
	}
	
	private static String trans2XMLFormat(String s) {
		s = s.replaceAll("&", "&amp;");
		s = s.replaceAll("\"", "&quot;");
		s = s.replaceAll("<", "&lt;");
		s = s.replaceAll(">", "&gt;");
		s = s.replaceAll("'", "&apos;");
		return s;
	}
	
	private static String back(String s) {
		s = s.replaceAll("&amp;", "&");
		s = s.replaceAll("&quot;", "\"");
		s = s.replaceAll("&lt;", "<");
		s = s.replaceAll("&gt;", ">");
		s = s.replaceAll("&apos;", "'");
		return s;
	}
	
	public List getAllItems(Document document){
		Element root = document.getRootElement();
		for(Iterator i = root.elementIterator("item");i.hasNext();){
			Element element = (Element) i.next();
			System.out.println(((Element)(element.selectSingleNode("title"))).getTextTrim());
		}
		List list = document.selectNodes("//channel/item");
		ArrayList l = new ArrayList();
		Item item = null;
		for(int i = 0;i<list.size();i++){
			Element ele = (Element) list.get(i);
			String title = ((Element)(ele.selectSingleNode("title"))).getTextTrim();
			Node node = ele.selectSingleNode("category");
			String catalog = "";
			if(null!=node){
				catalog = ((Element)node).getTextTrim();
			}
			String content = ((Element)(ele.selectSingleNode("content:encoded"))).getTextTrim();
			String pubDate = ((Element)(ele.selectSingleNode("wp:post_date_gmt"))).getTextTrim();
			item = new Item();
			item.setTitle(title);
			item.setCatalog(catalog);
			item.setContent(content);
			item.setPostDate(pubDate);
			l.add(item);
		}
		return l;
	}
	
	public void createNewText(List list){
		list = (ArrayList)list;
		Item item = null;
		for(int i=0;i<list.size();i++){
			item = (Item)list.get(i);
			String title = item.getTitle();
			String pubDate = item.getPostDate();
			String category = item.getCatalog();
			String content = item.getContent();
			System.out.println("--------------------------------------------------------");
			System.out.println(back(title));
			System.out.println(back(pubDate));
			System.out.println(back(category));
			System.out.println(back(content));
			System.out.println();
		}
	}
	
	public static void main(String[] args) throws DocumentException, IOException {
		//testDom4j td = new testDom4j();
		//td.createNewText(td.getAllItems(td.parse("c:\\undo.xml")));
		
		maina();
	}
	
	public static void maina() throws DocumentException, IOException {
		String fileName = File.separator + "file";// + File.separator
				//+ "book.xml";
		
		System.out.println(fileName);
		File f = new File(fileName);
		System.out.println(f.createNewFile());
		
		//Document doc = read(fileName);
		//System.out.println(doc.getXMLEncoding());
	}

	public static Document read(String fileName) throws DocumentException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File(fileName));
		return document;
	}
}
